Skip to content

Conversation

AronNovak
Copy link

This rule detects when a class uses both a trait A and trait B, where trait A already uses trait B (either directly or transitively).

Features

  • Detects direct redundancy (class uses A and B, A uses B)
  • Detects transitive redundancy (class uses A and C, A uses B, B uses C)
  • Configurable via strictRules.noRedundantTraitUse parameter

Example

trait BarTrait {}
trait FooTrait { use BarTrait; }

class MyClass {
    use FooTrait;
    use BarTrait; // Error: redundant, already included via FooTrait
}

@AronNovak AronNovak changed the title Add NoRedundantTraitUseRule to detect redundant trait usage Add rule to detect redundant trait usage Aug 28, 2025
@ondrejmirtes
Copy link
Member

Hi,
I don't think this is a very common and bug-inducing problem. Additionally, there might be valid reasons to do this if you account for trait adaptations (with insteadof and as keywords).

As usual, feel free to publish this as your own package, or try to pitch this idea to other maintainers of strict rules packages like https://github.com/shipmonk-rnd/phpstan-rules or https://github.com/ergebnis/phpstan-rules 😊

@AronNovak
Copy link
Author

@ondrejmirtes It's problematic performance-wise. On a client project, phpstan needed 7 minutes to complete. After eliminating excessive redundant trait use, we were down to ~3 minutes. It can be a huge performance penalty for phpstan, and the reason is obvious, the trait is evaluated in every possible context where it appears. This redundant trait usage is problematic for this reason. Also it indicates bad design, but it's opinionated.

@ondrejmirtes
Copy link
Member

Please report this in phpstan/phpstan as a performance bug with a reproducer.

@AronNovak
Copy link
Author

phpstan/phpstan#13520

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants